package com.zhl.study.getThreadResult;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @description Future接口
 * Future接口通常与ExecutorService一起使用,用于获取异步执行的任务结果。通过调用Future.get()可以获取任务返回值或抛出的异常。
 * @Author: z156940l@outlook.com
 * @className FutureWay
 * @Date: 2024/5/23 10:37
 */
@Slf4j
public class FutureWay {

    public static void testFuture() throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        Future<String> future = executor.submit(new Callable<String>() {
            public String call() throws Exception {
                // 线程执行的代码
                return "Result";
            }
        });

        String result = future.get(); // 获取线程执行的结果
        log.info(result);
    }
}
